#include "mvc/view/settings_dialog.h"
#include "ui_settings_dialog.h"
#include <QFileDialog>
#include <QSignalMapper>
#include <iostream>
#include "common/settings.h"

#define WORKSPACE_DIR 0

Settings_dialog::Settings_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Settings_dialog)
{
    ui->setupUi(this);
    closed = false;
    load_image();
    load_directories();
    connect_buttons();

}

Settings_dialog::~Settings_dialog()
{
    delete ui;
}

/*
 * Set the text on the widget
 */
void Settings_dialog::retranslateUi(){
    ui->retranslateUi(this);
}

/**
 * @brief Settings_dialog::load_image
 * Load a backward image on the widget
 */
void Settings_dialog::load_image(){
    QImage settings_image(":images/background_setting.png");
    ui->image->setPixmap(QPixmap::fromImage(settings_image));
    ui->image->setScaledContents(true);

}

/**
 * @brief Settings_dialog::load_directories
 * Loads the directories of the settings
 */
void Settings_dialog::load_directories(){
    if (projects_path.isEmpty() || airfoils_path.isEmpty() || workspace_path.isEmpty())
        workspace_path = workspace_directory();

    projects_path = workspace_path + "projects/";
    airfoils_path = workspace_path + "airfoils/";

    ui->workspace_lineEdit->setText(workspace_path);
    QString temp_text = ui->projects_label->text();
    int last_index = temp_text.lastIndexOf(":");
    temp_text.remove(last_index + 1, temp_text.length());
    temp_text.append(projects_path);
    ui->projects_label->setText(temp_text);
    temp_text = ui->airfoil_label->text();
    last_index = temp_text.lastIndexOf(":");
    temp_text.remove(last_index + 1, temp_text.length());
    temp_text.append(airfoils_path);
    ui->airfoil_label->setText(temp_text);

}

/**
 * @brief Settings_dialog::select_directory
 * Actions to do when 'select' button is pressed
 */
void Settings_dialog::select_directory(){
    workspace_path = select_directory_dialog() + "/";
    load_directories();

}

/**
 * @brief Settings_dialog::ok_pressed
 * Actions to do when 'ok' button is pressed.
 */
void Settings_dialog::ok_pressed(){
    set_workspace_path(workspace_path);
    closed = true;
}

/**
 * @brief Settings_dialog::select_directory_dialog
 * @return path of the new directory selected
 */
QString Settings_dialog::select_directory_dialog(){
    QString path;
    path = projects_directory();

    QString dir_name = QFileDialog::getExistingDirectory(this, tr("Open Directory"),path, QFileDialog::ShowDirsOnly
                                                         | QFileDialog::DontResolveSymlinks);
    if (dir_name.isNull()) {
        dir_name = path;
    }
    return dir_name;
}

/**
 * @brief Settings_dialog::connect_buttons
 * Connect each button with a signal.
 */
void Settings_dialog::connect_buttons(){
    //QSignalMapper* signalMapper = new QSignalMapper (this) ;
    connect (ui->workspace_pushButton,SIGNAL(clicked()), this, SLOT(select_directory())) ;

    //signalMapper -> setMapping (ui->workspace_pushButton, WORKSPACE_DIR) ;

    //connect (signalMapper, SIGNAL(mapped(int)), this, SLOT(select_directory(int))) ;
    connect (ui->buttonBox, SIGNAL(accepted()), this, SLOT(ok_pressed()));
}
